from pathlib import Path
import shutil
import re

with open('sample.txt') as f:
    d={}
    for word in f.read().lower().split():
        if word in d:
            d[word]+=1
        else:
            d[word]=1
    lst=sorted(d.items(),key=lambda x:x[1],reverse=True)
    for i in range(10):
        print(lst[i])


print('处理后：')
with open('sample.txt') as f:
    d={}
    #使用re模块findall查找单词
#d[word]=d.get(word,0)+1
    for word in re.findall("[a-z]+",f.read().lower()):
        if word in d:
            d[word]+=1
        else:
            d[word]=1
    lst=sorted(d.items(),key=lambda x:x[1],reverse=True)
    for i in range(10):
        print(lst[i])


# 处理单词这里，可以试试别的方式
